package org.haifi.game.mina;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.haifi.game.reflection.GameReflect;

import com.google.protobuf.MessageLite;

/**
 * TestCase专用，用来向服务器发包
 * 
 * 
 * 
 * @author joycube2
 * 
 */
public class GameClientEncoderFilter extends ProtocolEncoderAdapter {

	@Override
	public void encode(IoSession session, Object message,
			ProtocolEncoderOutput out) throws Exception {
		// TODO Auto-generated method stub
		// GameCoderObject commandCoderObject = (GameCoderObject) message;
		GameTestCaseCoderObject obj = (GameTestCaseCoderObject) message;
		MessageLite messageLite = obj.messageLite;
		byte[] protoData = messageLite.toByteArray();
		IoBuffer buffer = IoBuffer.allocate(protoData.length + 5);
		buffer.putShort((short) protoData.length);
		buffer.putShort(obj.cmdId);
		buffer.put((byte) 0);// TODO:这里有必要发送loop数据吗
		buffer.put(protoData);
		buffer.flip();
		out.write(buffer);
	}

}
